$def with (list)

$ title = list.name
$ title_with_site = _("%(name)s | Lists | Open Library", name=title)
$ page = safeint(query_param('page'), 1) - 1
$ page_size = 50
$ meta_cover_url = None # Add metatag for image at end of this template, if a cover image is present
$ meta_description = title
$if list.description:
    $ meta_description = _("%(title)s: %(description)s", title=title, description=list.description)

$var title: $list.name

$putctx("description", meta_description)

$add_metatag(property="twitter:card", content="summary")
$add_metatag(property="twitter:site", content="@openlibrary")
$add_metatag(property="twitter:title", content=title_with_site)
$add_metatag(property="twitter:description", content= _("View the list on Open Library."))

$add_metatag(property="og:title", content=title_with_site)
$add_metatag(property="og:type", content="website")
$add_metatag(property="og:url", content=request.canonical_url)
$add_metatag(property="og:site_name", content="Open Library")
$add_metatag(property="og:description", content=meta_description)

$set_share_links(url=request.canonical_url, title=title, view_context=ctx)

$jsdef render_seed_count(seed_count):
    $ungettext("1 seed", "%(count)d seeds", seed_count, count=seed_count)

<div id="contentHead" style="margin-bottom:0;">

    $:macros.databarView(list)

    <div class="superNav">
        $ owner = list.get_owner()
        <a href="$owner.key">$owner.displayname</a>
        /
        <a href="$owner.key/lists">$_('Lists')</a>
    </div>
    <h1>$list.name</h1>
    <p class="collapse sansserif">
        <span class="darkgreen"><strong>$:render_seed_count(len(list.seeds))</strong></span>
    </p>
</div>

<script type="text/javascript">
<!--
window.q.push(function(){
    \$('ul#listResults li:last').css('border-bottom','1px dotted #ebebeb');
});

function remove_seed(list_key, seed, success) {
    if (seed[0] == "/") {
        seed = {"key": seed}
    }

    \$.ajax({
        type: "POST",
        url: list_key + "/seeds.json",
        contentType: "application/json",
        data: JSON.stringify({
            "remove": [seed]
        }),
        dataType: "json",

        beforeSend: function(xhr) {
            xhr.setRequestHeader("Content-Type", "application/json");
            xhr.setRequestHeader("Accept", "application/json");
        },
        success: success
    });
}

function get_seed_count() {
    return \$("ul#listResults").children().length;
}

window.q.push(function(){
    \$(".listDelete a").click(function() {
        var seed_id = \$(this).closest("li").attr("id");

        if (get_seed_count() > 1) {
            \$("#remove-seed-dialog")
                .data("seed-id", seed_id)
                .dialog("open");
            \$("#remove-seed-dialog").removeClass('hidden');
        }
        else {
            \$("#delete-list-dialog").dialog("open");            
            \$("#delete-list-dialog").removeClass('hidden');
        }

    });

    \$("#remove-seed-dialog").dialog({
        autoOpen: false,
        width: 400,
        modal: true,
        resizable: false,
        buttons: {
            "$:_('Yes, I\'m sure')": function() {
                var list_key = $:json_encode(list.key);
                var seed_id = "#" + \$(this).data("seed-id");
                var seed = \$(seed_id).find("span.seed-key").html();

                var _this = this;

                remove_seed(list_key, seed, function() {
                    \$(seed_id).remove();
                    // update seed count
                    \$("#seed-count").html(render_seed_count(get_seed_count()));

                    // TODO: update edition count

                    \$(_this).dialog("close");
                    \$("#remove-seed-dialog").addClass('hidden');
                });
            },
            "$_('No, cancel')": function() {
                \$(this).dialog("close");
                \$("#remove-seed-dialog").addClass('hidden');
            }
        }
    });

    \$("#delete-list-dialog").dialog({
        autoOpen: false,
        width: 400,
        modal: true,
        resizable: false,
        buttons: {
            "$:_('Yes, I\'m sure')": function() {
                var list_key = $:json_encode(list.key);
                var _this = this;

                \$.post(list_key + "/delete.json", function() {
                    \$(_this).dialog("close");
                    window.location.reload();
                });
            },
            "$_('No, cancel')": function() {
                \$(this).dialog("close");
            }
        }
    });
});

//-->
</script>

<div id="contentBody">
    <div id="remove-seed-dialog" class="hidden" title="$_('Remove seed')">$_('Are you sure you want to remove this item from the list?')</div>
    <div id="delete-list-dialog" class="hidden" title="$_('Remove Seed')">
        $_('You are about to remove the last item in the list. That will delete the whole list. Are you sure you want to continue?')
    </div>

    $def render_authors(authors):
        $if not authors:
            <em>$_('Unknown authors')</em>
        $else:
            $for a in authors:
                <a href="$a.url()" class="results">$a.name</a>$cond(loop.last, "", ", ")

    <div class="mybooks-list">

        $if list.description:
            $:format(list.description)

        <ul id="listResults" class="list-books">
          $for seed in list.get_seeds(sort=True)[page*page_size:page*page_size+page_size]:
            <li id="seed-$loop.index" class="searchResultItem">
                $ default_image = "https://openlibrary.org/images/icons/avatar_book-sm.png"
                $ cover = seed.get_cover()
                $if cover:
                    $ cover_url = item_image(cover.url("M"), default=default_image)
                $else:
                    $ cover_url = default_image

                $# Use first found image as the OpenGraph image
                $if not meta_cover_url:
                    $ meta_cover_url = cover_url
                <span class="bookcover">
                    <a href="$seed.url">
                      <img src="$cover_url"/>
                    </a>
                </span>
                <span class="seed-key hidden">$seed.key</span>
                <span class="details">
                    <span class="resultTitle">
                        $ ebook = {}
                        $if seed.type in ['edition', 'work']:
                            $ ebook = seed.document.get_ebook_info()
                            $ doc = seed.document
                            <!-- $:json_encode(ebook) -->
                        <h3 class="booktitle">
                            <a href="$seed.url" class="results">$seed.title</a>
                        </h3>
                        $ work_editions_count = 0
                        $if seed.type in ['edition', 'work']:
                            $if seed.type == "work":
                                $ authors = seed.document.get_authors()
                            $else:
                                $if seed.document.works:
                                    $ work_editions_count = seed.document.works[0].edition_count
                                    $ authors = seed.document.works[0].get_authors()
                                $else:
                                    $ authors = seed.document.get_authors()
                            <div class="author">$:_('by %(name)s', name=render_authors(authors))</div>
                        <span class="meta">
                          <!-- FIXME: I18N using internal seed.type directly for display is not internationalizable -->
                          <span class="type small">$seed.type</span>
                        $if seed.type == "edition" and seed.document.get('works'):
                            $:ungettext('- <a href="%(url)s">1 edition</a> in catalog', '- <a href="%(url)s">%(count)s editions</a> in catalog', seed.work_count, count=commify(work_editions_count), url=seed.document.works[0].url())
                        $elif seed.type == "work":
                            $ungettext('- 1 edition', '- %(count)s editions', seed.edition_count, count=commify(seed.edition_count))
                        $else:
                            $ungettext('- 1 work', '- %(count)s works', seed.work_count, count=commify(seed.work_count))
                        </span>

                        $if 'daisy_url' in ebook:
                            <div class="type small"><a href="$ebook['daisy_url']" title="$_('Protected DAISY')">$_('Download DAISY eBook for print disabled')</a></div>

                        $if seed.last_update:
                            <span class="time">
                                $:_('Last updated <span>%(date)s</span>', date=datestr(seed.last_update))
                            </span>
                    </span>
                </span>
                <span class="searchResultItemCTA">
                    $if ctx.user and ctx.user.key == list.get_owner().key:
                        <span class="listDelete sansserif smaller">
                          <a href="javascript:;"><span></span>$_('Remove this item?')</a>
                        </span>
                    $if 'daisy_url' in ebook:
                        <a class="cta-btn print-disabled-only hidden" href="//archive.org/download/$(doc.ia[0])/$(doc.ia[0])_daisy.zip" title="$_('Protected DAISY')">
                          <span class="actions">
                            <span class="image daisy"></span>
                          </span>
                          <span class="label">$_('Download DAISY')</span>
                        </a>
                    <span class="searchResultItemCTA-lending">
                    </span>
                </span>
            </li>
        </ul>
    </div>

    <div class="contentOnethird" style="margin-bottom:0;">

        $:render_template("type/list/exports", list)

        <h3 class="collapse">$_('List Metadata')</h3>
        <div class="smaller lightgreen sansserif">$_('Derived from seed metadata')</div>
        <br/>

        $def render_subjects(label, subjects):
            $if subjects:
                <div class="section">
                    <h6 class="collapse black uppercase">$label</h6>
                    <div class="sansserif">
                    $for subject in subjects:
                        <a href="$subject.url">$subject.title</a>$cond(not loop.last, ",", "")
                    </div>
                </div>

        $ subjects = list.get_subjects()

        $:render_subjects(_("Subjects"), subjects.subjects)
        $:render_subjects(_("People"), subjects.people)
        $:render_subjects(_("Places"), subjects.places)
        $:render_subjects(_("Times"), subjects.times)
    </div>

    <div class="clearfix"></div>

    $:macros.Pager(page+1, len(list.seeds), page_size)


    $:render_template("lib/history", list)
</div>

$if meta_cover_url is not None:
    $add_metatag(property="og:image", content=meta_cover_url)
    $add_metatag(property="twitter:image", content=meta_cover_url)
